<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>小方块</title>
  <style>
    body{
      background:rgb(38, 37, 43);
    }
   #container{
      width: 1200px;
      margin: 100px auto;
      display: flex;
      flex-wrap: wrap;
      
    }
    .square{
      height: 20px;
      width: 20px;
      margin-right: 2px;
      margin-bottom: 2px;
      background-color: rgb(52, 49, 49)
    }
  </style>
</head>
<body>
  <div class="container" id="container">

  </div>
  <script>
    generateSquare();
    //生成小方块阵列，并且将每一个正方形区域都绑定上鼠标移动事件。
    function generateSquare() {
      const container = document.getElementById("container");
      
      for(let i = 0; i < 3186; i++){
        const  square = document.createElement("div");
        square.className = "square"  
        container.appendChild(square);
       }
       container.onmouseover = function(e){
          let target = e.target;
          if(target.className == "square"){
            setColor(target);
          }
       }
       container.onmouseout = function(e){
          let target = e.target;
          if(target.className == "square"){
            deleteColor(target);
          }
       }
    }

    function colorRandom(){
      let red = Math.floor(Math.random()*256);
      let blue = Math.floor(Math.random()*256);
      let green = Math.floor(Math.random()*256);
      return `rgb(${red},${blue},${green})`;
    }

    function setColor(element){
      element.style.backgroundColor = colorRandom();
    }
    function deleteColor(element){
      element.style.transition = '1s'
      element.style.backgroundColor = '' ;
    }
  </script>
</body>
</html>